package com.yimi.yinhepda.db.helper;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.os.Environment;

import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;
import com.pda.uce.commom.util.LogUtil;
import com.pda.ym.db.table.CarPlateTable;
import com.pda.ym.db.table.UnloadScanTable;
import com.pda.ym.utils.DateHelper;
import com.uce.pda.util.Autil;
import com.yimi.yinhepda.base.BaseApplication;
import com.yimi.yinhepda.db.dao.BreakCardTableDao;
import com.yimi.yinhepda.db.dao.BuildPckScanTableDao;
import com.yimi.yinhepda.db.dao.CheckScanTableDao;
import com.yimi.yinhepda.db.dao.DifferScanTableDao;
import com.yimi.yinhepda.db.dao.DispactchScanMasterTableDao;
import com.yimi.yinhepda.db.dao.DispactchScanTableDao;
import com.yimi.yinhepda.db.dao.JieLiuJianTableDao;
import com.yimi.yinhepda.db.dao.LanHuoChildTableDao;
import com.yimi.yinhepda.db.dao.LanHuoTableDao;
import com.yimi.yinhepda.db.dao.LoadUnloadGroupTableDao;
import com.yimi.yinhepda.db.dao.LoadUnloadTimeTableDao;
import com.yimi.yinhepda.db.dao.MoveStockScanMasterTableDao;
import com.yimi.yinhepda.db.dao.MoveStockScanTableDao;
import com.yimi.yinhepda.db.dao.MoveStockTaskCacheTableDao;
import com.yimi.yinhepda.db.dao.MyMessageDao;
import com.yimi.yinhepda.db.dao.OffLineUnbindTableDao;
import com.yimi.yinhepda.db.dao.ScanDetailVoDao;
import com.yimi.yinhepda.db.dao.SortScanTableDao;
import com.yimi.yinhepda.db.dao.UnloadScanTableDao;
import com.yimi.yinhepda.db.dao.UnloadTaskTableDao;
import com.yimi.yinhepda.db.dao.UnloadUcTaskNoTableDao;
import com.yimi.yinhepda.db.dao.UserReadMessageDao;
import com.yimi.yinhepda.db.table.BreakCardTable;
import com.yimi.yinhepda.db.table.BuildPckScanTable;
import com.yimi.yinhepda.db.table.CheckBalanceTable;
import com.yimi.yinhepda.db.table.CheckScanTable;
import com.yimi.yinhepda.db.table.CompanyTable;
import com.yimi.yinhepda.db.table.DepartmentTable;
import com.yimi.yinhepda.db.table.DifferScanTable;
import com.yimi.yinhepda.db.table.DispatchScanMasterTable;
import com.yimi.yinhepda.db.table.DispatchScanTable;
import com.yimi.yinhepda.db.table.ForkliftStatisticsTable;
import com.yimi.yinhepda.db.table.FuncBtnTable;
import com.yimi.yinhepda.db.table.JieLiuJianTable;
import com.yimi.yinhepda.db.table.LanHuoChildTable;
import com.yimi.yinhepda.db.table.LanHuoTable;
import com.yimi.yinhepda.db.table.LineVoTable;
import com.yimi.yinhepda.db.table.LoadUnLoadGroupTable;
import com.yimi.yinhepda.db.table.LoadUnloadTimeTable;
import com.yimi.yinhepda.db.table.MoveStockScanMasterTable;
import com.yimi.yinhepda.db.table.MoveStockScanTable;
import com.yimi.yinhepda.db.table.MoveStockTaskCacheTable;
import com.yimi.yinhepda.db.table.OffLineLoadUnloadGroupTable;
import com.yimi.yinhepda.db.table.OffLinePlatformNoTable;
import com.yimi.yinhepda.db.table.OffLineUnbindTable;
import com.yimi.yinhepda.db.table.QueryQcTypeTable;
import com.yimi.yinhepda.db.table.ScanDetailVo;
import com.yimi.yinhepda.db.table.SortScanTable;
import com.yimi.yinhepda.db.table.StorageDetailTable;
import com.yimi.yinhepda.db.table.StorageInfoTable;
import com.yimi.yinhepda.db.table.SyncServerDataTaskTable;
import com.yimi.yinhepda.db.table.UCDeptTable;
import com.yimi.yinhepda.db.table.UcWaybillRegexTable;
import com.yimi.yinhepda.db.table.UnloadTaskTable;
import com.yimi.yinhepda.db.table.UnloadUcTaskNoTable;
import com.yimi.yinhepda.db.table.UserInfoTable;
import com.yimi.yinhepda.entity.message.MyMessageBean;
import com.yimi.yinhepda.entity.message.UserReadMessageBean;

import java.io.File;
import java.sql.SQLException;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 需要实现该抽象类类进行通过实体进行创建表
 *
 * @author 000432
 */
public class DbHelper extends OrmLiteSqliteOpenHelper {

    public static final String TAG = DbHelper.class.getName();

    private AtomicInteger mOpenCounter = new AtomicInteger();
    /**
     * 数据库版本号
     */
    public static final int TABLE_VERSION = 44;

    /**
     * 数据库文件名称
     */
    public static final String TABLE_NAME = "ym-pda.db";
    private final static String StroagePath = Environment.getExternalStorageDirectory() + "/YM/";
    public final static String SqlitPath = StroagePath + TABLE_NAME;
    /**
     * 数据库文件夹
     */
    public static final String DB_FILE_DIR = "database";
    /**
     * 数据库路径
     */
    public static String DB_PATH;

    private volatile static DbHelper INSTANCE;

    static {
        File mFile = BaseApplication.getContext().getExternalFilesDir(DB_FILE_DIR);
        if (mFile != null) {
            DB_PATH = mFile.getAbsolutePath() + File.separator;
            if (!mFile.exists()) {
                if (mFile.mkdirs()) {
                    LogUtil.e("初始化database到内存卡成功");
                } else {
                    DB_PATH = "";
                    LogUtil.e("初始化database到机身内存成功");
                }
            }
        }

        //删除原来东大的数据库文件，DbHelper初始化构造时会重新创建数据库
        File sqlitFile = new File(SqlitPath);
        if (sqlitFile.exists()) {
            sqlitFile.delete();
        }
    }

    public DbHelper(Context context) {
        super(context, DB_PATH + TABLE_NAME, null, TABLE_VERSION);
    }

    /**
     * 数据库Helper 获取实例化
     */
    public static DbHelper getInstance(Context context) {
        if (INSTANCE == null) {
            synchronized (DbHelper.class) {
                if (INSTANCE == null) {
                    INSTANCE = new DbHelper(context);
                }
            }
        }
        return INSTANCE;
    }

    @Override
    public void onCreate(SQLiteDatabase database, ConnectionSource connectionSource) {
        try {
            // 创建 装卸车扫描明细表
            TableUtils.createTableIfNotExists(connectionSource, UnloadScanTable.class);
            // 创建 卸车任务表
            TableUtils.createTableIfNotExists(connectionSource, UnloadTaskTable.class);
            //建包扫描表
            TableUtils.createTableIfNotExists(connectionSource, BuildPckScanTable.class);
            //盘点扫描表
            TableUtils.createTableIfNotExists(connectionSource, CheckScanTable.class);
            //差异扫描表
            TableUtils.createTableIfNotExists(connectionSource, DifferScanTable.class);
            //账套表
            TableUtils.createTableIfNotExists(connectionSource, CompanyTable.class);
            //网点表
            TableUtils.createTableIfNotExists(connectionSource, DepartmentTable.class);
            //线路表
            TableUtils.createTableIfNotExists(connectionSource, LineVoTable.class);
            //库区表
            TableUtils.createTableIfNotExists(connectionSource, StorageInfoTable.class);
            //库区详细表
            TableUtils.createTableIfNotExists(connectionSource, StorageDetailTable.class);
            //问题件类型表
            TableUtils.createTableIfNotExists(connectionSource, QueryQcTypeTable.class);
            //分拣扫描表
            TableUtils.createTableIfNotExists(connectionSource, SortScanTable.class);
            //派送扫描表
            TableUtils.createTableIfNotExists(connectionSource, DispatchScanTable.class);
            //离线装卸车运单表
            TableUtils.createTableIfNotExists(connectionSource, ScanDetailVo.class);
            //服务端同步数据任务表
            TableUtils.createTableIfNotExists(connectionSource, SyncServerDataTaskTable.class);
            //集中派送主单表
            TableUtils.createTableIfNotExists(connectionSource, DispatchScanMasterTable.class);
            //复秤检查表
            TableUtils.createTableIfNotExists(connectionSource, CheckBalanceTable.class);
            //我的消息表
            TableUtils.createTableIfNotExists(connectionSource, MyMessageBean.class);
            //用户已读消息表
            TableUtils.createTableIfNotExists(connectionSource, UserReadMessageBean.class);
            //装卸组表
            TableUtils.createTableIfNotExists(connectionSource, LoadUnLoadGroupTable.class);
            //创建车牌表
            TableUtils.createTableIfNotExists(connectionSource, CarPlateTable.class);
            //PDA打卡表
            TableUtils.createTableIfNotExists(connectionSource, BreakCardTable.class);
            //装卸车时间提醒表
            TableUtils.createTableIfNotExists(connectionSource, LoadUnloadTimeTable.class);
            //离线托盘绑定表
            TableUtils.createTableIfNotExists(connectionSource, OffLineUnbindTable.class);
            //叉车分货统计表
            TableUtils.createTableIfNotExists(connectionSource, ForkliftStatisticsTable.class);
            //月台号表
            TableUtils.createTableIfNotExists(connectionSource, OffLinePlatformNoTable.class);
            //离线装卸组表
            TableUtils.createTableIfNotExists(connectionSource, OffLineLoadUnloadGroupTable.class);
            //用户信息表
            TableUtils.createTableIfNotExists(connectionSource, UserInfoTable.class);
            //移库装卸车扫描表
            TableUtils.createTableIfNotExists(connectionSource, MoveStockScanTable.class);
            //移库装卸车任务暂存表
            TableUtils.createTableIfNotExists(connectionSource, MoveStockTaskCacheTable.class);
            //移库装卸车扫描主表
            TableUtils.createTableIfNotExists(connectionSource, MoveStockScanMasterTable.class);
            //揽货扫描扫描表
            TableUtils.createTableIfNotExists(connectionSource, LanHuoTable.class);
            //揽货扫描子单表
            TableUtils.createTableIfNotExists(connectionSource, LanHuoChildTable.class);
            //功能按钮表
            TableUtils.createTableIfNotExists(connectionSource, FuncBtnTable.class);
            //卸车uc快递任务表
            TableUtils.createTableIfNotExists(connectionSource, UnloadUcTaskNoTable.class);
            //优速网点表
            TableUtils.createTableIfNotExists(connectionSource, UCDeptTable.class);
            //截留件表
            TableUtils.createTableIfNotExists(connectionSource, JieLiuJianTable.class);
            //快递运单规则表
            TableUtils.createTableIfNotExists(connectionSource, UcWaybillRegexTable.class);
            LogUtil.e("create table finish");
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase database, ConnectionSource connectionSource, int oldVersion, int newVersion) {
        LogUtil.e("oldVersion---------->" + oldVersion);
        if (oldVersion >= newVersion) {
            return;
        }
        try {
            if (oldVersion < 2) {
                //库区详细表
                TableUtils.createTableIfNotExists(connectionSource, StorageDetailTable.class);
            }
            if (oldVersion < 3) {
                //离线装卸车运单表
                TableUtils.createTableIfNotExists(connectionSource, ScanDetailVo.class);
                //配载单增加 序号列,业务类型列,列表显示时间的列
                DatabaseUtil.upgradeTable(database, connectionSource, UnloadTaskTable.class, DatabaseUtil.OperationType.ADD);
            }
            if (oldVersion < 4) {
                //配载单增加 结束时间，到达公司编码的列
                DatabaseUtil.upgradeTable(database, connectionSource, UnloadTaskTable.class, DatabaseUtil.OperationType.ADD);
            }
            if (oldVersion < 5) {
                //服务端同步数据任务表
                TableUtils.createTableIfNotExists(connectionSource, SyncServerDataTaskTable.class);
            }
            if (oldVersion < 6) {
                //添加是否自动上传图片字段
                DatabaseUtil.upgradeTable(database, connectionSource, QueryQcTypeTable.class, DatabaseUtil.OperationType.ADD);
            }

            if (oldVersion < 7) {
                TableUtils.dropTable(connectionSource, QueryQcTypeTable.class, true);
                TableUtils.createTableIfNotExists(connectionSource, QueryQcTypeTable.class);
            }
            if (oldVersion < 8) {
                //集中派送主单表
                TableUtils.createTableIfNotExists(connectionSource, DispatchScanMasterTable.class);
            }
            if (oldVersion < 9) {
                //添加必串货字段
                TableUtils.dropTable(connectionSource, UnloadScanTable.class, true);
                TableUtils.createTableIfNotExists(connectionSource, UnloadScanTable.class);
            }
            if (oldVersion < 10) {
                //添加复秤检查表
                TableUtils.createTableIfNotExists(connectionSource, CheckBalanceTable.class);
                //添加我的消息表
                TableUtils.createTableIfNotExists(connectionSource, MyMessageBean.class);
                //添加用户已读消息表
                TableUtils.createTableIfNotExists(connectionSource, UserReadMessageBean.class);
            }
            if (oldVersion < 11) {
                //添加任务号字段
                TableUtils.dropTable(connectionSource, BuildPckScanTable.class, true);
                TableUtils.createTableIfNotExists(connectionSource, BuildPckScanTable.class);
            }
            if (oldVersion < 12) {
                //添加参与人和参与人工号字段
                TableUtils.dropTable(connectionSource, UnloadScanTable.class, true);
                TableUtils.createTableIfNotExists(connectionSource, UnloadScanTable.class);
                //装卸组表
                TableUtils.createTableIfNotExists(connectionSource, LoadUnLoadGroupTable.class);
            }
            if (oldVersion < 13) {
                //添加参与人用户编码字段和月台号
                TableUtils.dropTable(connectionSource, LoadUnLoadGroupTable.class, true);
                TableUtils.createTableIfNotExists(connectionSource, LoadUnLoadGroupTable.class);

                //库区编码添加索引
                TableUtils.dropTable(connectionSource, StorageInfoTable.class, true);
                TableUtils.createTableIfNotExists(connectionSource, StorageInfoTable.class);
                //创建车牌表
                TableUtils.createTableIfNotExists(connectionSource, CarPlateTable.class);
                //PDA打卡表
                TableUtils.createTableIfNotExists(connectionSource, BreakCardTable.class);

                //添加网点类型字段:deptType
                DatabaseUtil.upgradeTable(database, connectionSource, UnloadScanTable.class, DatabaseUtil.OperationType.ADD);
            }
            if (oldVersion < 14) {
                //添加打卡日期字段:clockDate
                DatabaseUtil.upgradeTable(database, connectionSource, BreakCardTable.class, DatabaseUtil.OperationType.ADD);
            }
            if (oldVersion < 15) {
                //添加托盘号任务号字段:forliftNo和月台号字段platformNo
                DatabaseUtil.upgradeTable(database, connectionSource, UnloadScanTable.class, DatabaseUtil.OperationType.ADD);
                //添加参与人用户编码字段和月台号
                TableUtils.createTableIfNotExists(connectionSource, OffLineUnbindTable.class);
                //添加托盘绑定选择标志字段：palletBindCheck
                DatabaseUtil.upgradeTable(database, connectionSource, LoadUnLoadGroupTable.class, DatabaseUtil.OperationType.ADD);
            }
            if (oldVersion < 16) {
                //盘点扫描表添加滞留原因字段
                getDao(CheckScanTable.class).executeRaw("ALTER TABLE checkScanTable ADD COLUMN retention VARCHAR;");
            }
            if (oldVersion < 17) {
                //装卸车时间提醒表
                TableUtils.createTableIfNotExists(connectionSource, LoadUnloadTimeTable.class);
            }

            if (oldVersion < 18) {
                //添加叉车分货统计表
                TableUtils.createTableIfNotExists(connectionSource, ForkliftStatisticsTable.class);
            }

            if (oldVersion < 19) {
                //线路表添加latestTime索引
                getDao(LineVoTable.class).executeRaw("CREATE INDEX lineVoTable_latestTime_index ON lineVoTable (latestTime);");
            }

            if (oldVersion < 20) {
                //月台号表
                TableUtils.createTableIfNotExists(connectionSource, OffLinePlatformNoTable.class);
                //离线装卸组表
                TableUtils.createTableIfNotExists(connectionSource, OffLineLoadUnloadGroupTable.class);
                //新增封签号字段
                getDao(UnloadTaskTable.class).executeRaw("ALTER TABLE unloadTaskTable ADD COLUMN sealSignNo VARCHAR;");
                //新增装车类型字段
                getDao(UnloadTaskTable.class).executeRaw("ALTER TABLE unloadTaskTable ADD COLUMN loadType INTEGER;");
                //新增离线任务号字段
                getDao(UnloadTaskTable.class).executeRaw("ALTER TABLE unloadTaskTable ADD COLUMN offlineTaskNo VARCHAR;");
                getDao(UnloadScanTable.class).executeRaw("ALTER TABLE unloadScanTable ADD COLUMN offlineTaskNo VARCHAR;");
                getDao(ScanDetailVo.class).executeRaw("ALTER TABLE scanDetailVo ADD COLUMN offlineTaskNo VARCHAR;");
                //新增是否是整车字段
                getDao(UnloadTaskTable.class).executeRaw("ALTER TABLE unloadTaskTable ADD COLUMN isCompleteCar INTEGER;");
                //新增离线封车/解封时间
                getDao(UnloadTaskTable.class).executeRaw("ALTER TABLE unloadTaskTable ADD COLUMN sealSignTime VARCHAR;");
            }

            if (oldVersion < 21) {
                //添加用户信息表
                TableUtils.createTableIfNotExists(connectionSource, UserInfoTable.class);
            }

            if (oldVersion < 22) {
                //添加场地字段
                getDao(UnloadScanTable.class).executeRaw("ALTER TABLE unloadScanTable ADD COLUMN place VARCHAR;");
                //移库装卸车扫描表
                TableUtils.createTableIfNotExists(connectionSource, MoveStockScanTable.class);
                //移库装卸车任务暂存表
                TableUtils.createTableIfNotExists(connectionSource, MoveStockTaskCacheTable.class);
                //装卸组表添加车牌号
                getDao(LoadUnLoadGroupTable.class).executeRaw("ALTER TABLE LoadUnLoadGroupTable ADD COLUMN carNo VARCHAR;");
                //移库装卸车扫描主表
                TableUtils.createTableIfNotExists(connectionSource, MoveStockScanMasterTable.class);
                //问题件表添加字段
                TableUtils.dropTable(connectionSource, QueryQcTypeTable.class, true);
                TableUtils.createTableIfNotExists(connectionSource, QueryQcTypeTable.class);
            }
            if (oldVersion < 24) {
                //问题件表添加字段:isNeedToReply
                TableUtils.dropTable(connectionSource, QueryQcTypeTable.class, true);
                TableUtils.createTableIfNotExists(connectionSource, QueryQcTypeTable.class);
            }
            if (oldVersion < 25) {
                //揽货扫描扫描表
                TableUtils.createTableIfNotExists(connectionSource, LanHuoTable.class);
                //揽货扫描子单表
                TableUtils.createTableIfNotExists(connectionSource, LanHuoChildTable.class);
            }
            if (oldVersion < 26) {
                //装卸车扫描表添加产品类型字段
                getDao(UnloadScanTable.class).executeRaw("ALTER TABLE unloadScanTable ADD COLUMN productType VARCHAR;");
            }
            if (oldVersion < 27) {
                //装卸车扫描界面添加运单，任务号，配载单号索引
                getDao(UnloadScanTable.class).executeRaw("CREATE INDEX unloadScanTable_waybillNo_index ON unloadScanTable (waybillNo);");
                getDao(UnloadScanTable.class).executeRaw("CREATE INDEX unloadScanTable_task_index ON unloadScanTable (task);");
                getDao(UnloadScanTable.class).executeRaw("CREATE INDEX unloadScanTable_stowageNo_index ON unloadScanTable (stowageNo);");
                //集中配送主表添加createTime字段，用于15天后比较时间删除数据
                getDao(DispatchScanMasterTable.class).executeRaw("ALTER TABLE dispatchScanMasterTable ADD COLUMN createTime VARCHAR;");
                //离线解绑添加createTime字段，用于15天后比较时间删除数据
                getDao(OffLineUnbindTable.class).executeRaw("ALTER TABLE OffLineUnbindTable ADD COLUMN createTime VARCHAR;");
            }
            if (oldVersion < 28) {
                //库区表添加lastTime索引
                getDao(StorageInfoTable.class).executeRaw("CREATE INDEX storageInfoTable_lastTime_index ON storageInfoTable (lastTime);");
            }
            try {
                if (oldVersion < 30) {
                    //优速网点表
                    TableUtils.createTableIfNotExists(connectionSource, UCDeptTable.class);
                    //装卸车扫描表添加运单类型字段，车标号字段
                    getDao(UnloadScanTable.class).executeRaw("ALTER TABLE unloadScanTable ADD COLUMN waybillType INTEGER;");
                    getDao(UnloadScanTable.class).executeRaw("ALTER TABLE unloadScanTable ADD COLUMN carMark VARCHAR;");
                    //功能按钮表
                    TableUtils.createTableIfNotExists(connectionSource, FuncBtnTable.class);
                    //卸车任务表添加车标号字段
                    getDao(UnloadTaskTable.class).executeRaw("ALTER TABLE unloadTaskTable ADD COLUMN carMark VARCHAR;");
                    //卸车任务添加索引字段：任务号，配载单
                    getDao(UnloadTaskTable.class).executeRaw("CREATE INDEX unloadTaskTable_task_index ON unloadTaskTable (task);");
                    getDao(UnloadTaskTable.class).executeRaw("CREATE INDEX unloadTaskTable_stowageNo_index ON unloadTaskTable (stowageNo);");
                    //添加卸车uc快递任务表
                    TableUtils.createTableIfNotExists(connectionSource, UnloadUcTaskNoTable.class);
                }
            } catch (SQLException e) {
                LogUtil.e("sql异常--->" + e.getMessage());
            }
            if (oldVersion < 33) {
                //截留件表
                TableUtils.createTableIfNotExists(connectionSource, JieLiuJianTable.class);
                //装卸车扫描表添加超长件标识字段overLength
                getDao(UnloadScanTable.class).executeRaw("ALTER TABLE unloadScanTable ADD COLUMN overLength INTEGER;");
            }
            if (oldVersion < 34) {
                //开单件数字段 srcPiecesNum
                getDao(UnloadScanTable.class).executeRaw("ALTER TABLE unloadScanTable ADD COLUMN srcPiecesNum INTEGER;");
                //快运运单类型 kyWaybillType
                getDao(UnloadScanTable.class).executeRaw("ALTER TABLE unloadScanTable ADD COLUMN kyWaybillType INTEGER;");
                //签回单签收类型 singBackType
                getDao(UnloadScanTable.class).executeRaw("ALTER TABLE unloadScanTable ADD COLUMN singBackType VARCHAR;");
                //重量 weight
                getDao(UnloadScanTable.class).executeRaw("ALTER TABLE unloadScanTable ADD COLUMN weight VARCHAR;");
                //体积 volume
                getDao(UnloadScanTable.class).executeRaw("ALTER TABLE unloadScanTable ADD COLUMN volume VARCHAR;");
                //到付付款金额 paidPaymentAmount
                getDao(UnloadScanTable.class).executeRaw("ALTER TABLE unloadScanTable ADD COLUMN paidPaymentAmount VARCHAR;");
                //代收款 goodsChargeFee
                getDao(UnloadScanTable.class).executeRaw("ALTER TABLE unloadScanTable ADD COLUMN goodsChargeFee VARCHAR;");
                //是否自提 isZiTi
                getDao(UnloadScanTable.class).executeRaw("ALTER TABLE unloadScanTable ADD COLUMN isZiTi INTEGER;");
                //虚拟网点类型 数据字典key:virtual_node_type
                getDao(DepartmentTable.class).executeRaw("ALTER TABLE departmentTable ADD COLUMN column26 VARCHAR;");
                //所属分拨（编码）
                getDao(DepartmentTable.class).executeRaw("ALTER TABLE departmentTable ADD COLUMN belongForward VARCHAR;");
            }
            if (oldVersion < 35) {
                //跨越主单号
                getDao(UnloadScanTable.class).executeRaw("ALTER TABLE unloadScanTable ADD COLUMN kuayueWaybillNo VARCHAR;");
                //跨越子单号
                getDao(UnloadScanTable.class).executeRaw("ALTER TABLE unloadScanTable ADD COLUMN kuayueChildWaybillNo VARCHAR;");
                //货物名称
                getDao(UnloadScanTable.class).executeRaw("ALTER TABLE unloadScanTable ADD COLUMN goodsName VARCHAR;");
                //计费重量
                getDao(UnloadScanTable.class).executeRaw("ALTER TABLE unloadScanTable ADD COLUMN chargedWeight VARCHAR;");
                //交接件数
                getDao(UnloadScanTable.class).executeRaw("ALTER TABLE unloadScanTable ADD COLUMN pieceNum INTEGER;");
                //下一站编码
                getDao(UnloadScanTable.class).executeRaw("ALTER TABLE unloadScanTable ADD COLUMN nextDeptCode VARCHAR;");
                //下一站名称
                getDao(UnloadScanTable.class).executeRaw("ALTER TABLE unloadScanTable ADD COLUMN nextDeptName VARCHAR;");
            }
            if (oldVersion < 36) {
                //网点表添加deptCode为索引
                getDao(DepartmentTable.class).executeRaw("CREATE INDEX departmentTable_deptCode_index ON departmentTable (deptCode);");
            }
            if (oldVersion < 37) {
                //快递运单规则表
                TableUtils.createTableIfNotExists(connectionSource, UcWaybillRegexTable.class);
            }
            if (oldVersion < 38) {
                //上下一站
                getDao(UnloadScanTable.class).executeRaw("ALTER TABLE unloadScanTable ADD COLUMN relationDeptCode VARCHAR;");
                //上下一站名称
                getDao(UnloadScanTable.class).executeRaw("ALTER TABLE unloadScanTable ADD COLUMN relationDeptName VARCHAR;");
                //扫描表：上下一站部门类型
                getDao(UnloadScanTable.class).executeRaw("ALTER TABLE unloadScanTable ADD COLUMN relationDeptType VARCHAR;");
                //卸车任务表：上下一站部门类型
                getDao(UnloadTaskTable.class).executeRaw("ALTER TABLE unloadTaskTable ADD COLUMN relationDeptType VARCHAR;");

            }
            if (oldVersion < 39) {
                getDao(DepartmentTable.class).executeRaw("ALTER TABLE departmentTable ADD COLUMN isOmg INTEGER;");
            }
            if (oldVersion < 40) {
                try {
                    //上下一站
                    getDao(UnloadScanTable.class).executeRaw("ALTER TABLE unloadScanTable ADD COLUMN relationDeptCode VARCHAR;");
                    //上下一站名称
                    getDao(UnloadScanTable.class).executeRaw("ALTER TABLE unloadScanTable ADD COLUMN relationDeptName VARCHAR;");
                    //扫描表：上下一站部门类型
                    getDao(UnloadScanTable.class).executeRaw("ALTER TABLE unloadScanTable ADD COLUMN relationDeptType VARCHAR;");
                    //卸车任务表：上下一站部门类型
                    getDao(UnloadTaskTable.class).executeRaw("ALTER TABLE unloadTaskTable ADD COLUMN relationDeptType VARCHAR;");
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (oldVersion < 41) {
                //操作类型：1-装车。2-卸车
                getDao(UnloadTaskTable.class).executeRaw("ALTER TABLE unloadTaskTable ADD COLUMN operType INTEGER;");
                //车线任务号
                getDao(UnloadScanTable.class).executeRaw("ALTER TABLE unloadScanTable ADD COLUMN taskNo VARCHAR;");
                //电子称标示
                getDao(UnloadScanTable.class).executeRaw("ALTER TABLE unloadScanTable ADD COLUMN eleWeightFlag INTEGER;");
                //复称标示
                getDao(UnloadScanTable.class).executeRaw("ALTER TABLE unloadScanTable ADD COLUMN reWeighFlag VARCHAR;");
                //异形件（1-异形件，0为非异形件）
                getDao(UnloadScanTable.class).executeRaw("ALTER TABLE unloadScanTable ADD COLUMN specialShapeType VARCHAR;");
                try {
                    //快运/快递网点类型
                    getDao(DepartmentTable.class).executeRaw("ALTER TABLE departmentTable ADD COLUMN isOmg INTEGER;");
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                //功能表：组ID
                getDao(FuncBtnTable.class).executeRaw("ALTER TABLE FuncBtnTable ADD COLUMN groupId INTEGER;");
                //组code
                getDao(FuncBtnTable.class).executeRaw("ALTER TABLE FuncBtnTable ADD COLUMN groupCode VARCHAR;");
                //组名
                getDao(FuncBtnTable.class).executeRaw("ALTER TABLE FuncBtnTable ADD COLUMN groupName VARCHAR;");
                //是否已添加：0-未添加，1-已添加
                getDao(FuncBtnTable.class).executeRaw("ALTER TABLE FuncBtnTable ADD COLUMN isAdd INTEGER;");
                //部门类型：0-网点，1-分拨,2-通用
                getDao(FuncBtnTable.class).executeRaw("ALTER TABLE FuncBtnTable ADD COLUMN deptType INTEGER;");
                //功能类型：0-快运，1-快递
                getDao(FuncBtnTable.class).executeRaw("ALTER TABLE FuncBtnTable ADD COLUMN funcType INTEGER;");
            }
            if (oldVersion < 42) {
                //离线解绑添加type字段
                getDao(OffLineUnbindTable.class).executeRaw("ALTER TABLE OffLineUnbindTable ADD COLUMN type INTEGER;");
            }
            if (oldVersion < 43) {
                //新增车线任务号字段
                getDao(UnloadTaskTable.class).executeRaw("ALTER TABLE unloadTaskTable ADD COLUMN taskNo VARCHAR;");
            }
            if (oldVersion<44){
                 if (!Autil.checkColumnExists2(database,"unloadScanTable","isChangeDest")){
                     //新增车线任务号字段
                     getDao(UnloadScanTable.class).executeRaw("ALTER TABLE unloadScanTable ADD COLUMN isChangeDest INTEGER;");
                 }
            }
        } catch (SQLException e) {
            LogUtil.e("sql异常--->" + e.getMessage());
        }
    }

    @Override
    public void close() {
        super.close();
        INSTANCE = null;
    }

    /**
     * 清除15前的数据
     */
    public synchronized static void deleteData() {
        try {
            new BreakCardTableDao().deleteDataByDay("clockDateTime", -15);
            new BuildPckScanTableDao().deleteDataByDay("operTime", -15);
            new CheckScanTableDao().deleteDataByDay("operTime", -15);
            new DifferScanTableDao().deleteDataByDay("operTime", -15);
            new UnloadScanTableDao().deleteDataByDay("operTime", -15);
            new UnloadTaskTableDao().deleteDataByDay("createTime", -15);
            new UnloadUcTaskNoTableDao().deleteDataByDay("createTime", -15);
            new SortScanTableDao().deleteDataByDay("scanTime", -15);
            new DispactchScanTableDao().deleteDataByDay("operTime", -15);
            new DispactchScanMasterTableDao().deleteDataByDay("createTime", -15);
            new ScanDetailVoDao().deleteDataByDay("createTime", -15);
            new LoadUnloadGroupTableDao().deleteDataByDay("createTime", -15);
            new LoadUnloadTimeTableDao().deleteDataByDay("unloadFinishTimePoint", -15);
            new MoveStockScanMasterTableDao().deleteDataByDay("createTime", DateHelper.getOldDate(-15));
            new MoveStockScanTableDao().deleteDataByDay("createTime", DateHelper.getOldDate(-15));
            new MoveStockTaskCacheTableDao().deleteDataByDay("createTime", DateHelper.getOldDate(-15));
            new LanHuoTableDao().deleteDataByDay("scanTime", -15);
            new LanHuoChildTableDao().deleteDataByDay("scanTime", -15);
            new OffLineUnbindTableDao().deleteDataByDay("createTime", -15);
            new MyMessageDao().deleteDataByDay("create_time", -15);
            new UserReadMessageDao().deleteDataByDay("create_time", -15);
            new JieLiuJianTableDao().deleteDataByDay("createTime", -7);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
